package xuan.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

import org.roblybarger.*;

import org.w3c.dom.Element;

/**
 * TreeCellRenderer allow to custom node display
 * 
 * @author xuan
 *
 */
@SuppressWarnings("serial")
public class TreeCellCustomRenderer extends DefaultTreeCellRenderer {  
    /**
     * Color code : (GREEN,inserted), (RED,deleted), (ORANGE,updated) 
     */
    Color color1 = Color.GREEN;
    Color color2 = Color.RED;
    Color color3 = Color.ORANGE;
    Object insert=null;
    Object delete=null;
    Object update=null;
    Boolean isDad=false;//isDad indicates that operation takes place on this node or rather on its children.
    
    ImageIcon leafIcon;
    
    public TreeCellCustomRenderer(Object insert, Object delete, Object update, boolean isDad){
    	this.insert = insert;
    	this.delete = delete;
    	this.update = update;
    	this.isDad = isDad;
    	leafIcon = new ImageIcon("images/leafIcon.gif");
    	if (leafIcon != null) {
    	    setLeafIcon(leafIcon);
    	    setClosedIcon(leafIcon);
    	    setOpenIcon(leafIcon);
    	}    	
    }
     
    public Component getTreeCellRendererComponent(  
                        JTree tree,  
                        Object value,  
                        boolean sel,  
                        boolean expanded,  
                        boolean leaf,  
                        int row,  
                        boolean hasFocus) {  
   
        super.getTreeCellRendererComponent(  
                        tree, value, sel,  
                        expanded, leaf, row,  
                        hasFocus); 
   
        
        if(!isDad){
	        if ( isInsert(value)) { 	        	
	            setBackgroundNonSelectionColor(color1);
	            setBackgroundSelectionColor(color1);
	            
	        } 
	        else if (isDelete(value)) {  
	        	setBackgroundNonSelectionColor(color2);
	            setBackgroundSelectionColor(color2);   
	        }
	        else if (isUpdate(value)){
	        	setBackgroundNonSelectionColor(color3);
	            setBackgroundSelectionColor(color3);
	        }
	        else{
	        	setBackgroundSelectionColor(null);
	        	setBackgroundNonSelectionColor(null);
	        }
        }
        else{
        	if ( isInsert(value)) {  
	            setForeground(color1);
	        } 
	        else if (isDelete(value)) {  
	        	setForeground(color2);
	        }
	        else if (isUpdate(value)){
	        	setForeground(color3);
	        }
	        else{
	        	setForeground(Color.BLACK);
	        }
        }        
        return this;  
    }

	private boolean isUpdate(Object value) {
		if(update==null) return false;
		XMLTreeNode node1 = (XMLTreeNode)update;
		XMLTreeNode node2 = (XMLTreeNode)value;
		Element ele1 = node1.getElement();
		Element ele2 = node2.getElement();
		return (ele1==ele2);
	}

	private boolean isDelete(Object value) {
		if(delete==null) return false;
		XMLTreeNode node1 = (XMLTreeNode)delete;
		XMLTreeNode node2 = (XMLTreeNode)value;
		Element ele1 = node1.getElement();
		Element ele2 = node2.getElement();
		return (ele1==ele2);
	}

	private boolean isInsert(Object value) {
		if(insert==null) return false;
		XMLTreeNode node1 = (XMLTreeNode)insert;
		XMLTreeNode node2 = (XMLTreeNode)value;
		Element ele1 = node1.getElement();
		Element ele2 = node2.getElement();
		return (ele1==ele2);
	}  
}